package com.am.mosl.cityandroid.leftmenu;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.am.mosl.cityandroid.mainboard.ActivityDetailActivity;
import com.am.mosl.cityandroid.login.LoginActivity;
import com.am.mosl.cityandroid.R;
import com.am.mosl.cityandroid.activity.BaseActivity;
import com.am.mosl.cityandroid.bean.BData;
import com.am.mosl.cityandroid.constant.HttpConstant;
import com.am.mosl.cityandroid.message.LoginMessage;
import com.am.mosl.cityandroid.util.SettingUtils;
import com.am.mosl.cityandroid.widget.SharePopWindow;
import com.nostra13.universalimageloader.core.ImageLoader;

import de.greenrobot.event.EventBus;

/**
 * Created by mosl on 14-7-2.
 * 已报名活动
 */
public class ActivityedActivity extends BaseActivity {

	public static final String TAG = "ActivityedActivity";
	private ListView mActivityedListView;
	private List<BData> data = new ArrayList<BData>(); // 获取的 LIST 是个容器
	private ActivityedListViewAdapter adapter;

	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				adapter = new ActivityedListViewAdapter(ActivityedActivity.this);
				mActivityedListView.setAdapter(adapter);

				break;
			case 2:

				break;
			}
		};
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activityed_layout);
		initActionBar();
		mActivityedListView = (ListView) findViewById(R.id.activityed_list);
		
		judgeUserlogin();
		getUserActivityed();
		
		mActivityedListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				BData datah = adapter.getItem(arg2);
				String contentid = datah.getContentid();

                ActivityDetailActivity.ShareData shareData =new ActivityDetailActivity.ShareData();
                BData bData=(BData)mActivityedListView.getAdapter().getItem(arg2);
                shareData.title=bData.getTitle();
                shareData.des=bData.getIntroduction();
                shareData.imageUrl="";
                Bundle bundle=new Bundle();
                bundle.putSerializable("data",shareData);
				Intent i = new Intent(ActivityedActivity.this,
						ActivityDetailActivity.class);
				i.putExtra("Intent_To_ArticleDetail", contentid);
                i.putExtras(bundle);
				startActivity(i);
			}
			
		});

	}

	private void judgeUserlogin() {
		if (!SettingUtils.IsLogin(this)) {
			Toast.makeText(this, "未登录，请先登录", Toast.LENGTH_LONG).show();
			startActivity(new Intent(this, LoginActivity.class));
			finish();
			return;
		}
	}

	private void getUserActivityed() {
		FinalHttp finalHttp = new FinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("token", SettingUtils.getUserInfo(this).getToken());
		finalHttp.post(HttpConstant.ActivityedHttp, params,
				new AjaxCallBack<Object>() {
					@Override
					public void onSuccess(Object o) {
						super.onSuccess(o);
						try {
							JSONObject jsonObject = new JSONObject(o.toString());
							JSONArray jsonArray = (JSONArray) jsonObject
									.opt("data");

							Log.d("BDATA", jsonObject.toString());

							String status = (String) jsonObject.opt("status");
							String msg = (String) jsonObject.opt("msg");
							if (status.equals("1")) {
								data = JSON.parseArray(jsonArray.toString(),
										BData.class);
								mHandler.sendEmptyMessage(1);
							} 
							else if (status.equals("2")) {

								SettingUtils
										.RemoveAccount(ActivityedActivity.this);
								LoginMessage message = new LoginMessage();
								message.setType(LoginMessage.QUIT);
								EventBus.getDefault().post(message);

								Toast.makeText(ActivityedActivity.this,
										"帐号验证失败，请重新登录", Toast.LENGTH_SHORT)
										.show();
								ActivityedActivity.this.finish();
							}
							
							else {
								Toast.makeText(ActivityedActivity.this,
										"获取信息失败：" + msg, Toast.LENGTH_SHORT)
										.show();
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(Throwable t, int errorNo,
							String strMsg) {
						super.onFailure(t, errorNo, strMsg);
					}
				});
	}


	void initActionBar() {
		TextView tittle = (TextView) findViewById(R.id.tittle);
		tittle.setText("已报名活动");
		TextView RightTitle = (TextView) findViewById(R.id.right_tittle);
		RightTitle.setVisibility(View.GONE);
		View Back = (View) findViewById(R.id.layout_action_bar_back);
		Back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityedActivity.this.onBack();
			}
		});
	}

	class ActivityedListViewAdapter extends BaseAdapter {

		private Context context;

		public ActivityedListViewAdapter(Context context) {
			this.context = context;
		}

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public BData getItem(int position) {
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			final ViewHolder holder;
			final BData data1 = data.get(position);
			if (convertView == null) {
				holder = new ViewHolder();

				convertView = LayoutInflater.from(context).inflate(
						R.layout.main_activity_list_view_2, parent, false);

				holder.title = (TextView) convertView
						.findViewById(R.id.activity_title);
				holder.message = (TextView) convertView
						.findViewById(R.id.introduction);
				holder.image = (ImageView) convertView
						.findViewById(R.id.activity_image);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.title.setText(data1.getTitle());
			holder.message.setText(data1.getIntroduction());
			
			ImageLoader.getInstance().displayImage(data1.getImage(), holder.image);
            Log.d("Hello",data1.getImage());
			return convertView;

		}
	}

	class ViewHolder {
		public TextView title;
		public TextView message;
		public ImageView image;

	}

}